/**
 * @Author: 陈瑞鹏
 * @Date:   2021-11-08 19:19:24
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2022-12-06 09:28:29
 */

import { SoundResManager } from "../../ctrs/soundCtr";
import { INotification } from "../pureMvc/Interfaces";
import { MediatorComponent } from "./MediatorComponent";

const { ccclass, property } = cc._decorator;

@ccclass

export default class BaseGameComponent extends cc.Component {
    reFindTarget() {
        throw new Error("Method not implemented.");
    }
    @property({ tooltip: '按钮是否点击音效' })
    private isBtnSound: boolean = false;

    private btnArr: Array<cc.Button> = null;

    protected mediator: MediatorComponent = null;
    public m_bIsRunning: boolean = false; //工作状态
    public customData: any = null;

    public onEnable(): void {
        if (this.mediator) {
            this.mediator.registerMediator();
        }
        else {

            this.mediator = new MediatorComponent(this.getClassName(), this);
            this.mediator.registerMediator();
        }
        this.onAddToStage();
    }

    public onDisable(): void {
        this.mediator.removeMediator();
        if (this.btnArr && this.btnArr.length > 0) {
            for (let i = 0, count = this.btnArr.length; i < count; i++) {
                this.btnArr[i].node.off('click', this.btn_onEvent, this);
            }
        }
    }

    public onAddToStage(): void {

        if (this.isBtnSound) {
            this.btnArr = this.node.getComponentsInChildren(cc.Button);

            if (this.btnArr && this.btnArr.length > 0) {
                for (let i = 0, count = this.btnArr.length; i < count; i++) {
                    this.btnArr[i].node.on('click', this.btn_onEvent, this);
                    // Utils.addButtonEvent(this.btnArr[i],'btn_onEvent',this.btnArr[i].node,this.getClassName());
                }
            }
        }
        else {
            this.btnArr = null;
        }
    }


    private btn_onEvent() {
        SoundResManager.getInstance().Play('SoundClick');
    }

    getClassName() {
        return this.node.name;
    }

    public handleNotification(notification: INotification): void {

    }
    public listNotificationInterests(): Array<string> {
        return [];
    }

    public sendNotification(name: string, body: any = null, type: string = null): void {
        if (this.mediator)
            this.mediator.sendNotification(name, body, type);
    }

    public getDisplayView(): cc.Node {
        return this.node;
    }


    public setIsRunning(isRunning: boolean): void {
        this.m_bIsRunning = isRunning;
    }

    public getIsRunning(): boolean {
        return this.m_bIsRunning;
    }

    /**
* 本Item进入ScrollView的时候回调
*/
    onEnterSrcollView() {
        this.node.opacity = 255;
        // console.log("onEnterSrcollView")
    }

    /**
     * 本Item离开ScrollView的时候回调
     */
    onExitScrollView() {
        this.node.opacity = 0;
        // console.log("onExitScrollView")
    }
}